/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace ELE.Common.Util
{
  /// <summary>
  /// A utility class that uses reflection to instanciate objects
  /// </summary>
  public static class ObjectFactory
  {
    /// <summary>
    /// Creates an instance of the given type that is defined within the given assembly
    /// </summary>
    /// <param name="assemblyName">The assembly name where the type is compiled</param>
    /// <param name="typeName">The FQ name of the class</param>
    /// <returns></returns>
    public static object CreateInstance(string assemblyName, string typeName)
    {
      object wrapper = Activator.CreateInstance(assemblyName, typeName);
      return((System.Runtime.Remoting.ObjectHandle)wrapper).Unwrap();
    }
    /// <summary>
    /// Creates a generic dictionary class
    /// </summary>
    /// <param name="keysType">The type of the keys</param>
    /// <param name="valuesType">The type of the values</param>
    /// <returns>Instance of <see cref="System.Collections.Generic.Dictionary&lt;T,T&gt;"/></returns>
    public static object CreateGenericDictionary(string keysType, string valuesType)
    {
      return CreateGenericDictionary("System.Collections.Generic.Dictionary", keysType, valuesType);
    }
    /// <summary>
    /// Creates a custom generic dictionary class
    /// </summary>
    /// <param name="dictionaryType">The type of dictionary</param>
    /// <param name="keysType">The type of the keys</param>
    /// <param name="valuesType">The type of the values</param>
    /// <returns>Instance of <see cref="System.Collections.Generic.Dictionary&lt;T,T&gt;"/></returns>
    public static object CreateGenericDictionary(string dictionaryType, string keysType, string valuesType)
    {
      string tname = dictionaryType + "`2[[" + keysType + "],[" + valuesType + "]]";
      Type constructed = Type.GetType(tname, true);
      return Activator.CreateInstance(constructed);
    }
    /// <summary>
    /// Creates an instance of a generic list
    /// </summary>
    /// <param name="valuesType">The type of the values</param>
    /// <returns>Instance of <see cref="System.Collections.Generic.List&lt;T&gt;"/></returns>
    public static object CreateGenericList(string valuesType)
    {
      return CreateGenericList("System.Collections.Generic.List", valuesType);
    }
    /// <summary>
    /// Creates an instance of a custom generic list
    /// </summary>
    /// <param name="listType">The type of generic list</param>
    /// <param name="valuesType">The type of the values</param>
    /// <returns>Instance of <see cref="System.Collections.Generic.List&lt;T&gt;"/></returns>
    public static object CreateGenericList(string listType, string valuesType)
    {
      string tname = listType + "`1[[" + valuesType + "]]";
      Type constructed = Type.GetType(tname, true);
      return Activator.CreateInstance(constructed);
    }
  }
}
